 <template>
    <div>
        <el-row>
            <!--检索-->
            <el-form :inline="true" :model="search" class="demo-form-inline">
                <el-input v-if="false" v-model="search.pageNum"></el-input>
                <el-input v-if="false" v-model="search.pageSize"></el-input>
                <el-col :span="4">
                    <el-input v-model="search.name" placeholder="请输入品牌名称"></el-input>
                </el-col>
                <el-col :span="12"
                    ><div>
                        <el-button type="primary" icon="el-icon-search" @click="searchBtn">搜索</el-button>
                        <el-button type="info" icon="el-icon-loading" @click="emptyBtn">刷新</el-button>
                    </div>
                </el-col></el-form
            >
            <el-button type="primary" @click="addBtn" icon="el-icon-circle-plus">新增</el-button>
            <!--功能列表-->
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="index" label="#" sortable> </el-table-column>

                <el-table-column prop="name" label="品牌名称"> </el-table-column>
                <el-table-column prop="firstLetter" label="首字母"> </el-table-column>
                <el-table-column prop="sort" label="排序等级"></el-table-column>
                <el-table-column prop="factoryStatus" label="是否为品牌制造商" :formatter="getStatusName"> </el-table-column>
                <el-table-column prop="showStatus" label="品牌状态" :formatter="getStatusName1"> </el-table-column>
                <el-table-column prop="brandStory" label="品牌故事"> </el-table-column>

                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button size="small" @click="updateInfo(scope.$index, scope.row)" icon="el-icon-edit" type="success"
                            >编辑</el-button
                        >
                        <el-button size="small" @click="deleteInfo(scope.$index, scope.row)" icon="el-icon-delete" type="danger"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <!--分页功能-->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.pageNum"
                :page-sizes="[1, 5, 10, 20, 100]"
                :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
            >
            </el-pagination>
            <!--新增/修改 Form -->
            <el-dialog title="品牌管理" :visible.sync="editInfoState">
                <el-form :model="formInfo">
                    <el-form-item label="品牌名称" :label-width="formLabelWidth">
                        <el-input v-model="formInfo.name" placeholder="品牌名称" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="首字母" :label-width="formLabelWidth">
                        <el-input v-model="formInfo.firstLetter" placeholder="首字母" auto-complete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="排序等级" :label-width="formLabelWidth">
                        <el-select v-model="formInfo.sort" placeholder="请选择">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否为品牌制造商" :label-width="formLabelWidth">
                        <el-radio v-model="formInfo.factoryStatus" :label="1">有效</el-radio>
                        <el-radio v-model="formInfo.factoryStatus" :label="0">无效</el-radio>
                    </el-form-item>
                    <el-form-item label="品牌状态" :label-width="formLabelWidth">
                        <el-radio v-model="formInfo.showStatus" :label="1">有效</el-radio>
                        <el-radio v-model="formInfo.showStatus" :label="0">无效</el-radio>
                    </el-form-item>
                    <el-form-item label="品牌故事" :label-width="formLabelWidth">
                        <el-input v-model="formInfo.brandStory" placeholder="品牌故事" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editInfoState = false">取 消</el-button>
                    <el-button type="primary" @click="saveInfo">确 定</el-button>
                </div>
            </el-dialog>
        </el-row>
    </div>
</template>

<script>
import Vue from 'vue';
import axios from 'axios';
import VueAxios from 'vue-axios';
Vue.use(VueAxios, axios);

export default {
    data() {
        return {
            tableData: [],
            powerData: [],
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            page: {},
            formInfo: {},
            search: { pageNum: 1, pageSize: 5 },
            formLabelWidth: '130px',
            editInfoState: false,
            options: [
                {
                    value: '1',
                    label: '1'
                },
                {
                    value: '2',
                    label: '2'
                },
                {
                    value: '3',
                    label: '3'
                },
                {
                    value: '4',
                    label: '4'
                },
                {
                    value: '5',
                    label: '5'
                }
            ]
        };
    },
    created: function () {
        this.load();
    },
    methods: {
        //初始化数据
        load() {
            //页面初始化时，加载的方法
            this.$http.post("/car/brand/queryAllByParam",this.search).then((response) => {
                    console.log("=========================="+response)
                    this.tableData = response.data.data.list;
                    this.page = response.data.data;
                    this.total = response.data.data.total;
                });
        },
        //每页显示数量发生变化
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.search.pageSize = val;
            this.load();
        },
        //第几页发生变化
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.search.pageNum = val;
            this.load();
        },
        //查询
        searchBtn() {
            console.log(`当前页`);
            this.load();
        },
        //刷新
        emptyBtn() {
            this.search = {};
            this.search.pageNum = 1;
            this.search.pageSize = 5;
            this.load();
        },
        getStatusName(row, column, cellValue) {
            return cellValue == 1 ? '是' : '否';
        },
        getStatusName1(row, column, cellValue) {
            return cellValue == 1 ? '有效' : '无效';
        },
        addBtn() {
            this.formInfo = {};
            this.editInfoState = true;
        },
        updateInfo: function (index, row) {
            this.formInfo = row;
            this.editInfoState = true;
        },
        saveInfo: function () {
            var _this = this;
            if (_this.formInfo.id != null && _this.formInfo.id != '' && _this.formInfo.id != undefined) {
                axios
                    .post('/car/brand/update', _this.formInfo)
                    .then(function (response) {
                        //提示成功
                        _this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });
                        //正确请求时，回调函数
                        console.log(response);
                        _this.load();
                        _this.editInfoState = false;
                    })
                    .catch(function (error) {
                        _this.$message.error('你错了。。。');
                        _this.editInfoState = false;
                        _this.load();
                        //失败处理
                        console.log(error);
                    });
            } else {
                axios
                    .post('/car/brand/insert', _this.formInfo)
                    .then(function (response) {
                        //提示成功
                        _this.$message({
                            type: 'success',
                            message: '新增成功!'
                        });
                        //正确请求时，回调函数
                        _this.load();
                        _this.editInfoState = false;
                    })
                    .catch(function (error) {
                        _this.$message.error('你错了。。。');
                        //失败处理
                        console.log(error);
                    });
            }
        },
        deleteInfo: function (index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    var _this = this;
                    axios
                        .delete('/car/brand/deleteById?id=' + row.id)
                        .then(function (response) {
                            if (response.status == 200) {
                                //提示成功
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                //重新加载数据
                                _this.load();
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                            _this.$message.error('你错了。。。');
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        }
    }
};
</script>
<style>
</style>